//
//  Mycollection_ViewController.m
//  Food
//
//  Created by dlios on 15-7-20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "Mycollection_ViewController.h"
#import "BaseHeader.h"
#import "HomePage_detailed_Cell.h"
#import "SQL_DataBase.h"
#import "HomePage_detailed_model.h"
#import "HomePage_display_Controller.h"
#import "Adventure_TableViewCell.h"
#import "AdventureModel.h"
#import "AdventureDetailVController.h"
static int i = 0;
@interface Mycollection_ViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UISegmentedControl *seg;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *CollectArry;

@property (nonatomic, retain) NSMutableArray *ActivityArry;

@end

@implementation Mycollection_ViewController

-(void)dealloc
{
    [_tableView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = YES;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    i = 0;
    [super viewDidLoad];
     //Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround"]];
    self.navigationItem.title = @"收藏";
    
    [self createSegmented];
    [self crecteCollection];
    [self creatCollectData];
    [self createTableView];
    [self creatActivityData];
    
    self.tableView.hidden = YES;
    
    [self deleteRight];
}

- (void) deleteRight
{
    if (self.tableView.hidden == YES) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleDone target:self action:@selector(deleteCollection)];
    }
    else
    {
        self.navigationItem.rightBarButtonItem = nil;
    }
}

- (void) deleteCollection
{
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"删除"]) {
        i = 1;
        self.navigationItem.rightBarButtonItem.title = @"完成";
        [self.collectionView reloadData];
    } else if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"完成"]) {
        i = 0;
        self.navigationItem.rightBarButtonItem.title = @"删除";
        [self.collectionView reloadData];
    }
}

#warning CollectionView
- (void) crecteCollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth - 30) / 2.0, 120.0 / 375 *ScreenWidth);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    layout.minimumLineSpacing = 5;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 50.0 / 375 *ScreenWidth, ScreenWidth, ScreenHeight - 64 - 49 - 50.0 / 375 *ScreenWidth) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[HomePage_detailed_Cell class] forCellWithReuseIdentifier:@"collectionCell"];
}


- (void) creatCollectData
{
    self.CollectArry = [NSMutableArray array];
    SQL_DataBase *dbManager = [SQL_DataBase shareInstance];
    [dbManager openDB];
    [dbManager creatTable1];
    self.CollectArry = [dbManager selectCollect];
    [self.collectionView reloadData];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        HomePage_detailed_Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
        HomePage_detailed_model *model = self.CollectArry[indexPath.item];
    if (i == 1) {
        cell.button.userInteractionEnabled = YES;
        cell.button.hidden = NO;
        cell.button.tag = 1000 + indexPath.item;
        [cell.button addTarget:self action:@selector(fun1:) forControlEvents:UIControlEventTouchUpInside];
    }
    if (i == 0) {
        cell.button.userInteractionEnabled = NO;
        cell.button.hidden = YES;
    }
        [cell setModel:model];
        return cell;
}

- (void) fun1:(UIButton *) button
{
    HomePage_detailed_model *model = self.CollectArry[button.tag - 1000];
    
    SQL_DataBase *dbManager = [SQL_DataBase shareInstance];
    [dbManager openDB];
    [dbManager creatTable1];
    [dbManager deleteCollect:model.classId];
    [self creatCollectData];
    [self.collectionView reloadData];
    
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"删除成功" delegate:self cancelButtonTitle:nil  otherButtonTitles:@"确定", nil];
    [alterView show];
    [alterView release];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.CollectArry.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    HomePage_display_Controller *display = [[HomePage_display_Controller  alloc] init];
    HomePage_detailed_model *model = self.CollectArry[indexPath.item];
    display.classid = model.classId;
    display.classTitle = model.name;
    display.MVBackGroundimage = model.imagePath;
    [self.navigationController pushViewController:display animated:YES];
}

#warning TableView
- (void) createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 50.0 / 375 *ScreenWidth, ScreenWidth, ScreenHeight - 64 - 49 - 50.0 / 375 *ScreenWidth) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate =self;
    [self.view addSubview:self.tableView];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        AdventureModel *adventure = self.ActivityArry[indexPath.row];
        SQL_DataBase *dbManager = [SQL_DataBase shareInstance];
        [dbManager openDB];
        [dbManager creatTable2];
        [dbManager deleteActivity:adventure.freshId];
        
        [self.ActivityArry removeObjectAtIndex:indexPath.row];
        NSArray *indexArr = @[indexPath];
        [self.tableView deleteRowsAtIndexPaths:indexArr withRowAnimation:UITableViewRowAnimationTop];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"确认删除";
}

- (void) creatActivityData
{
    self.ActivityArry = [NSMutableArray array];
    SQL_DataBase *dbManager = [SQL_DataBase shareInstance];
    [dbManager openDB];
    [dbManager creatTable2];
    self.ActivityArry = [dbManager selectActivity];
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Adventure_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"adventure"];
    if (!cell) {
        cell = [[Adventure_TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"adventure"];
    }
    AdventureModel *adventure = self.ActivityArry[indexPath.row];
    cell.adventure = adventure;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return self.ActivityArry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return 120.0 / 375 * ScreenWidth;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AdventureModel *model = self.ActivityArry[indexPath.row];
    AdventureDetailVController *adventureDetail = [[AdventureDetailVController alloc] init];
    adventureDetail.freshid = model.freshId;
    adventureDetail.string = model.name;
    adventureDetail.content = model.content;
    adventureDetail.titleImageFile = model.titleImageFile;
    [self.navigationController pushViewController:adventureDetail animated:YES];
}

- (void)createSegmented
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 50.0 / 375 *ScreenWidth)];
    view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    [self.view addSubview:view];
    
    self.seg = [[[UISegmentedControl alloc] initWithItems:@[@"视频收藏", @"活动收藏"]] autorelease];
    self.seg.frame = CGRectMake(0, 0, 200 / 375.0 * ScreenWidth, 30 / 375.0 * ScreenWidth);
    self.seg.center = CGPointMake(ScreenWidth / 2.0 , view.frame.size.height / 2.0);
    self.seg.selectedSegmentIndex = 0;
     [self.seg addTarget:self action:@selector(doSomethingInSegment:)forControlEvents:UIControlEventValueChanged];
    [view addSubview:self.seg];
    
}


-(void)doSomethingInSegment:(UISegmentedControl *)Seg
{
    if (self.seg.selectedSegmentIndex == 0) {
        self.tableView.hidden = YES;
        self.collectionView.hidden = NO;
        [self deleteRight];
        [self.collectionView reloadData];
    }
    if (self.seg.selectedSegmentIndex == 1) {
        self.collectionView.hidden = YES;
        self.tableView.hidden= NO;
        [self deleteRight];
        [self.tableView reloadData];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
